package com.litxuf.util;

import java.util.Random;

import org.apache.commons.lang.StringUtils;

public class StringTools {
	/**
	 * 生成的格式为 (5,6,4,3,1)
	 * 
	 * @param values
	 * @return
	 */
	public static String arrayToStringWithBrackets(String values[]) {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				if (i == values.length - 1)
					sb.append(values[i]);
				else
					sb.append(values[i] + ",");
			}
			sb.append(")");
		}
		return sb.toString();
	}

	/**
	 * 生成的格式为 5,6,4,3,1
	 * 
	 * @param values
	 * @return
	 */
	public static String arrayToStringNotWithBrackets(String values[]) {
		StringBuffer sb = new StringBuffer();

		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				if (i == values.length - 1)
					sb.append(values[i]);
				else
					sb.append(values[i] + ",");
			}
		}
		return sb.toString();
	}

	/**
	 * 截取字符串
	 * 
	 * @param value
	 * @param lengthShown
	 * @return
	 */
	public static String shorten(String value, int lengthShown) {

		if (value == null)
			return " ";
		if (value != null)
			value = value.trim();
		int len = value.length();
		if (lengthShown >= len) {
			return value;
		} else {
			return value.substring(0, lengthShown) + "...";
		}
	}
	
	
	public static String shortenWithout(String value, int lengthShown) {

		if (value == null)
			return " ";
		if (value != null)
			value = value.trim();
		int len = value.length();
		if (lengthShown >= len) {
			return value;
		} else {
			return value.substring(0, lengthShown);
		}
	}

	/**
	 * 判断数组是否包含某个元素 made in 2010-3-28下午10:50:43
	 * 
	 * @param code
	 * @param codes
	 * @return
	 * 
	 */
	public static boolean isContain(String code, String codes[]) {
		if (codes == null) {
			return false;
		}
		for (String key : codes) {
			if (code.equals(key)) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		// System.out.println(checkEmail("xx@cc.cnc.cn"));
		getRandomPwd();
		for (int x = 0; x < 6; x++) {
			getRandomPwd();
		}
	}

	public static boolean checkEmail(String mail) {
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(regex);
		java.util.regex.Matcher m = p.matcher(mail);
		return m.find();
	}

	public static String nullToString(String str) {
		if (StringUtils.isBlank(str)) {
			return "";
		}
		return str;
	}

	
	public static String getMemberType(String membertype) {
		if (membertype == null) {
			return "未知";
		} else if (membertype.equals("0")) {
			return "个人会员";
		} else if (membertype.equals("1")) {
			return "企业会员";
		} else if (membertype.equals("2")) {
			return "企业子会员";
		} else {
			return "其他";
		}
	}
	/**
	 * 获取随机密码 made in 2010-6-2下午02:23:31
	 * 
	 * @param membertype
	 * @return
	 * 
	 */
	public static String getRandomPwd() {
		StringBuffer sb = new StringBuffer();
		String str = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ";
		Random r = new Random();
		for (int x = 0; x < 6; x++) {
			int j = r.nextInt(57);
			sb.append(str.charAt(j));
		}
		System.out.println(sb.toString());
		return sb.toString();
	}
}
